package com.macro.mall.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.macro.mall.service.BmsFeaturedStoreService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.BmsFeaturedStoreDto;
import com.macro.mall.dto.BmsFeaturedStoreParam;


@Controller
@Api(tags = "bmsFeaturedStore")
@Tag(name = "bmsFeaturedStore", description = "添加特色商店")
@RequestMapping("/bmsFeaturedStore")
public class BmsFeaturedStoreController {

    @Autowired
    private BmsFeaturedStoreService bmsFeaturedStoreService;
    
    @ApiOperation("添加特色商店")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> create(@RequestBody BmsFeaturedStoreParam bmsFeaturedStoreParam) {
        int count = bmsFeaturedStoreService.create(bmsFeaturedStoreParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    
    @ApiOperation("修改特色商店")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> update(@RequestBody BmsFeaturedStoreParam bmsFeaturedStoreParam) {
        int count = bmsFeaturedStoreService.update(bmsFeaturedStoreParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    
    @SuppressWarnings("unchecked")
    @ApiOperation("分页特色商店")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<BmsFeaturedStoreDto>> getList(@RequestBody BmsFeaturedStoreParam bmsFeaturedStoreParam) {
        Map<String,Object> map = bmsFeaturedStoreService.getList(bmsFeaturedStoreParam);
        List<BmsFeaturedStoreDto> bmsFeaturedStoreDtoList = (List<BmsFeaturedStoreDto>) map.get("bmsFeaturedStoreDtoList");
        Long total = (Long) map.get("total");
        int pages = (int) map.get("pages");
        CommonPage<BmsFeaturedStoreDto> result = CommonPage.restPage(bmsFeaturedStoreDtoList);
        result.setTotal(total);
        result.setTotalPage(pages);
        return CommonResult.success(result);
    }
    
    @ApiOperation("根据id获取特色商店")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<BmsFeaturedStoreDto> getItem(@PathVariable Long id) {
        BmsFeaturedStoreDto bmsFeaturedStoreDto = bmsFeaturedStoreService.getItem(id);
        return CommonResult.success(bmsFeaturedStoreDto);
    }
    
    @ApiOperation("删除特色商店")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> delete(@RequestParam("ids") List<Long> ids) {
        int count = bmsFeaturedStoreService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
}
